using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Lavka.Models;

namespace Lavka.Controllers
{
    [Authorize(Roles = "Administrators")]
    public class CustomersController : Controller
    {
        private LavkaDbEntities context = new LavkaDbEntities();

        //
        // GET: /Customers/

        public ViewResult Index()
        {
            return View(context.Customers.Include("Orders").ToList());
        }

        //
        // GET: /Customers/Details/5

        public ViewResult Details(int id)
        {
            Customers customers = context.Customers.Single(x => x.CustomerId == id);
            return View(customers);
        }

        //
        // GET: /Customers/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Customers/Create

        [HttpPost]
        public ActionResult Create(Customers customers)
        {
            if (ModelState.IsValid)
            {
                context.Customers.AddObject(customers);
                context.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(customers);
        }
        
        //
        // GET: /Customers/Edit/5
 
        public ActionResult Edit(int id)
        {
            Customers customers = context.Customers.Single(x => x.CustomerId == id);
            return View(customers);
        }

        //
        // POST: /Customers/Edit/5

        [HttpPost]
        public ActionResult Edit(Customers customers)
        {
            if (ModelState.IsValid)
            {
                context.Customers.Attach(customers);
                context.ObjectStateManager.ChangeObjectState(customers, EntityState.Modified);
                context.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(customers);
        }

        //
        // GET: /Customers/Delete/5
 
        public ActionResult Delete(int id)
        {
            Customers customers = context.Customers.Single(x => x.CustomerId == id);
            return View(customers);
        }

        //
        // POST: /Customers/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Customers customers = context.Customers.Single(x => x.CustomerId == id);
            context.Customers.DeleteObject(customers);
            context.SaveChanges();
            return RedirectToAction("Index");
        }
    }
}